#!/bin/bash
typeid=6
host='10.61.13.96'
db='recommendationengine_fuzhou'

charset='--default-character-set=utf8'
fetchLeftIdSQL="select programid into outfile '/tmp/left_id' from program where typeid=$typeid"
fetchRightIdSQL="
select programid into outfile '/tmp/right_id' from program where name like
'%熊出没之过年-国语版%' or name like 
'%朵拉美人鱼王国探险-国语版%' or name like 
'%朵拉童话王国探险-国语版%' or name like 
'%铠甲勇士之帝皇侠-国语版%' or name like 
'%猫和老鼠别碰胡须-国语版%' or name like 
'%赛车总动员-中文版%' or name like 
'%赛尔号之寻找凤凰神兽-国语版%' or name like 
'%朵拉生日探险1-国语版%' or name like 
'%机器人总动员-国语版%' or name like 
'%超人总动员-国语版%' or name like 
'%喜羊羊与灰太狼之牛气冲天-国语%' or name like 
'%恐龙宝贝-国语版%' or name like 
'%怪物电力公司-国语版%' or name like 
'%飞屋环游记-国语版%' or name like 
'%洛克王国圣龙骑士-原声版%' or name like 
'%机器人总动员-原声版%' or name like 
'%闪电狗-国语版%' or name like 
'%高飞狗大电影-国语版%' or name like 
'%飞屋环游记-原声版%' or name like 
'%小熊维尼-国语版%' or name like 
'%美食总动员-国语版%' or name like 
'%超人总动员-原声版%' or name like 
'%名侦探柯南漆黑的追踪者-原声%' or name like 
'%玩具总动员3-中文版%' or name like 
'%猪猪侠之窘窘危机-国语版%' or name like 
'%魔发奇缘-中文版%' or name like 
'%沉默的15分钟-原声版%' or name like 
'%玩具总动员-国语版%' or name like 
'%白雪公主-原声版%' or name like 
'%灰姑娘-原声版%' or name like 
'%跳跳虎历险记-国语版%' or name like 
'%海底总动员-中文版%' or name like 
'%摩尔庄园冰世纪-原声版%' or name like 
'%鬼神传-原声版%' or name like 
'%圣诞夜惊魂-原声版%' or name like 
'%玩具总动员2-中文版%' or name like 
'%恐龙-国语版%' or name like 
'%美女与野兽-中文版%' or name like 
'%蓝精灵-中文版%' or name like 
'%魔发奇缘-原声版%' or name like 
'%闪电狗-原声版%' or name like 
'%小熊维尼之长鼻怪大冒险-国语%' or name like 
'%魁拔-原声版%' or name like 
'%大都会-原声版%' or name like 
'%美食总动员-原声版%' or name like 
'%影视巨星总动员-中文版%' or name like 
'%新大闹天宫-原声版%' or name like 
'%忍者神龟-中文版%' or name like 
'%玩具总动员-原声版%' or name like 
'%蜡笔小新之动感超人大战高叉魔-国语版    %' or name like 
'%蜡笔小新之大人帝国的反击-国语版%' or name like 
'%蜡笔小新之搞怪游乐园大冒险-国语版  %' or name like 
'%奥特曼贝利亚银河帝国-国语版 %' or name like 
'%喜羊羊灰太狼-喜气羊羊过蛇年-国语版 %' or name like 
'%猫和老鼠巨人大冒险-原声版%' or name like 
'%赛罗奥特曼外传流星的誓言-国语版%';"

date
echo '[info] start job' 
rm -f /tmp/left_id 
rm -f /tmp/right_id
echo '[info] fetching right IDs...'
mysql -uroot -pmysql -h$host $db -e "$fetchLeftIdSQL"
echo '[info] fetching left IDs...'
mysql -uroot -pmysql -h$host $db $charset -e"$fetchRightIdSQL"
echo '[info] reading left ID...'
i=0
while read line
do
    leftIdArr[i]=$line
    i=$((i+1))
done < /tmp/left_id
echo "[info] totoal number of left ID fetched: ${#leftIdArr[@]}"
echo '[info] reading right ID...'
i=0
while read line
do
    rightIdArr[i]=$line
    i=$((i + 1))
done < /tmp/right_id
echo "[info] totoal number of right ID fetched: ${#rightIdArr[@]}"
echo '[info] start to insert manual recommendation rules IN SEQUENCE...'
i=0
mod=${#rightIdArr[@]}
for leftId in ${leftIdArr[@]}
do
    index=$(($i % $mod))
    echo '****************************************************************'
    echo "[info] current i:$i, index:$index"
    echo '****************************************************************'
    rightId=${rightIdArr[index]}
    echo "[info] deleting manual recommendation rules for left ID: $leftId"
    mysql -uroot -pmysql -h$host $db -e"delete from manualprogram where leftprogram='$leftId'"
    echo "[info] inserting manual recommendation for left ID: $leftId, and right ID: $rightId"
    mysql -uroot -pmysql -h$host $db -e"insert into manualprogram values(0, '$leftId', '$rightId', 1)"
    i=$((i + 1))
done

echo '[info] Job is done!!! Please check manualprogram table for verification...' 
date

